package com.lark.wifi.dialog;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cdl.library.config.DialogConfig;
import com.lark.wifi.base.IWifi;
import com.lark.wifi.base.WifiManager;
import com.mtk.launcher.R;
import com.mtk.launcher.util.shared.SharedPerManager;

/**
 * 对已保存的wifi 进行相应的操作
 */
public class WifiHandleDialog extends Dialog implements View.OnClickListener {

    private OnButtonListener buttonListener;
    private IWifi data;
    Context context;

    public WifiHandleDialog(@NonNull Context context) {
        super(context, R.style.CustomDialogTheme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_wifi_handle);
        initView();
    }

    TextView tv_cancel_save;
    TextView tv_link;

    protected void initView() {
        tv_cancel_save = findViewById(R.id.tv_cancel_save);
        tv_cancel_save.setOnClickListener(this);
        tv_link = findViewById(R.id.tv_link);
        tv_link.setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        TextView dialog_title = findViewById(R.id.dialog_title);
        TextView tv_status = findViewById(R.id.tv_status);
        TextView tv_signal = findViewById(R.id.tv_signal);
        TextView tv_pway = findViewById(R.id.tv_pway);
        if (data != null) {
            tv_status.setText(data.isConnected() ? getContext().getString(R.string.connected) : getContext().getString(R.string.not_connect));
            dialog_title.setText(data.name());
            String singleStr = WifiManager.signalLevToStr(context, data.level());
            tv_signal.setText(singleStr);
            tv_pway.setText(data.encryption());
        }
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = SharedPerManager.getScreenWidth() / 2;
        params.height = ActionBar.LayoutParams.WRAP_CONTENT;
        initFocusButton();
    }

    private void initFocusButton() {
        View.OnKeyListener onKeyListener = new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_DPAD_DOWN:
                            switch (v.getId()) {
                                case R.id.tv_cancel_save:
                                    tv_link.requestFocus();
                                    break;
                            }
                            break;
                        case KeyEvent.KEYCODE_DPAD_UP:
                            switch (v.getId()) {
                                case R.id.tv_link:
                                    tv_cancel_save.requestFocus();
                                    return true;
                            }
                            break;
                    }
                }
                return false;
            }
        };
        tv_cancel_save.setOnKeyListener(onKeyListener);
        tv_link.setOnKeyListener(onKeyListener);
    }


    @Override
    public void onClick(View v) {
        if (buttonListener != null) {
            buttonListener.onButton(v.getId());
        }
        dismiss();
    }

    public WifiHandleDialog setData(IWifi data) {
        this.data = data;
        return this;
    }

    public WifiHandleDialog setCenterListener(OnButtonListener listener) {
        this.buttonListener = listener;
        return this;
    }

    public interface OnButtonListener {
        void onButton(int buttonId);
    }
}
